// 在 .cpp 文件中进行函数定义
#include "Header.h"
BearSSL::WiFiClientSecure espClient;
PubSubClient pubSubClient(espClient);
void debugLog(const char* format, ...) {
#ifdef DEBUG_ENABLED
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
#endif
}
void serialPrintln( Printable& str){
    Serial.println(str);
}
std::string parseStringArg(const std::string& arg){
    return  arg;
}
// 获取设备的 MAC 地址并转换为字符串
String getUniqueClientId() {
    byte mac[6];
    WiFi.macAddress(mac);
    String macStr = "";
    for (unsigned char i : mac) {
        macStr += String(i, HEX);
    }
    return "ESP8266_" + macStr;
}
void syncTime() {
    configTime((const long)GlobalConstants::gmt_offset_sec , (const int)GlobalConstants::daylight_offset_sec, (const char *)GlobalConstants::ntp_server);
    debugLog("Waiting for NTP time sync: ");
    while (time(nullptr) < 8 * 3600 * 2) {
        delay(1000);
        debugLog(".");
    }
    debugLog("Time synchronized");
    struct tm timeinfo;
    if (getLocalTime(&timeinfo)) {
        GlobalConstants::syncTimeOk.store(1);;
        debugLog("Current time: %s",asctime(&timeinfo));
    } else {
        debugLog("Failed to obtain local time");
    }
}
int parseIntArg(const std::string& arg) {
    try {
        return std::stoi(arg);
    } catch (const std::invalid_argument& e) {
        debugLog("Error parsing integer from '%s': Invalid argument\n", arg.c_str());
        return 0;
    } catch (const std::out_of_range& e) {
        debugLog("Error parsing integer from '%s': Out of range\n", arg.c_str());
        return 0;
    }catch (...) {
        // 处理其他异常
        debugLog("Error parsing integer from '%s': Unknown exception\n", arg.c_str());
        return 0;
    }
}
unsigned int parseUnsignedIntArg(const std::string& arg) {
    return static_cast<unsigned int>(std::stoul(arg));
}

unsigned long parseUnsignedLongArg(const std::string& arg) {
    try {
        return std::stoul(arg);
    } catch (const std::invalid_argument& e) {
        debugLog("Error parsing unsigned long from '%s': Invalid argument\n", arg.c_str());
        return 0;
    } catch (const std::out_of_range& e) {
        debugLog("Error parsing unsigned long from '%s': Out of range\n", arg.c_str());
        return 0;
    }catch (...) {
        // 处理其他异常
        debugLog("Error parsing integer from '%s': Unknown exception\n", arg.c_str());
        return 0;
    }
}


bool parseBoolArg(const std::string& arg) {
    return arg == "true" || arg == "1";
}
